package org.example.threadobjectclasscommonmethods;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description: 展示sleep方法不释放lock锁
 * @author: MoodAir
 * @Date: 2020/5/16 0016 23:52
 */
public class SleepDontReleaseLock implements Runnable {

  private final Lock lock = new ReentrantLock();

  public static void main(String[] args) {
    SleepDontReleaseLock target = new SleepDontReleaseLock();
    new Thread(target).start();
    new Thread(target).start();
  }

  @Override
  public void run() {
    syn();
  }

  /**
   * @Author: MoodAir
   * @Description: 睡眠方法
   * @Date: 2020/5/16 0016 23:55
   * @param:
   * @return: void
   * @Version: 1.0
   */
  private void syn() {
    lock.lock();
    try {
      System.out.println(Thread.currentThread().getName() + "获取到锁");
      Thread.sleep(5000);
      System.out.println(Thread.currentThread().getName() + "释放到锁");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }
}
